package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.R;
import com.example.reggie.dto.DishDto;
import com.example.reggie.entity.Category;
import com.example.reggie.entity.Dish;
import com.example.reggie.entity.DishFlavor;
import com.example.reggie.service.impl.CategoryServiceImpl;
import com.example.reggie.service.impl.DishFlavorServiceImpl;
import com.example.reggie.service.impl.DishServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    DishServiceImpl dishService;

    @Autowired
    DishFlavorServiceImpl dishFlavorService;

    @Autowired
    CategoryServiceImpl categoryService;


    /**
     * 新增菜品
     *
     * @param
     */
    @PostMapping()
    public R<String> save(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return R.success("新增成功");
    }

    /**
     * 查询所有菜品
     *
     * @param
     */
    @GetMapping("/page")
    public R<IPage> getall(int page, int pageSize, String name) {
        IPage ipage = new Page(page, pageSize);
        IPage dtopage = new Page();
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<DishDto> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Dish::getName, name);
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(ipage, queryWrapper);

        BeanUtils.copyProperties(ipage, dtopage, "records");

        List<Dish> records = ipage.getRecords();
        List<DishDto> list = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);

            Long categoryId = item.getCategoryId();
            Category obj = categoryService.getById(categoryId);
            if (obj != null) {
                String catename = obj.getName();
                dishDto.setCategoryName(catename);
            }
            return dishDto;
        }).collect(Collectors.toList());
        dtopage.setRecords(list);
        return R.success(dtopage);
    }

    /**
     * 查询一个菜品
     *
     * @param
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id) {
        Dish dish = dishService.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(list);
        return R.success(dishDto);
    }

    /**
     * 修改一个菜品
     *
     * @param
     */
    @PutMapping()
    public R<String> update(@RequestBody DishDto dishDto) {
        log.info("DishDto展示:{}", dishDto.toString());
        boolean b = dishService.updateWithFlavor(dishDto);
        if (!b) {
            return R.error("修改失败");
        }
        return R.success("修改成功");
    }


    /**
     * 启停售菜品
     *
     * @param "拦截到请求：/dish/status/status [1600771040805441538,1397849739276890114]
     */
    @PostMapping("/status/{status}")
    public R<String> close(String id, @PathVariable int status) {
        log.info("获取的想要售的id:{}", id);
        log.info("status:{}", status);
        //通用方法
        boolean flage = dishService.updateWithStatus(id, status);
        if (!flage) {
            return R.error("修改失败");
        }
        return R.success("修改成功");
    }

    /**
     * 删除菜品
     *
     * @param "拦截到请求：/dish/
     */
    @DeleteMapping()
    public R<String> del(String ids) {
        log.info("要删除的id:{}", ids);
        dishService.delMulity(ids);
        return R.success("删除成功");
    }

    /**
     * 展示一类菜品
     *
     * @param "拦截到请求：/dish/
     */
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish) {
        Long categoryId = dish.getCategoryId();
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryId != null, Dish::getCategoryId, categoryId);
        queryWrapper.eq(Dish::getStatus, 1);
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(queryWrapper);
        List<DishDto> list1 = list.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Long categoryId1 = dish.getCategoryId();
            Category category = categoryService.getById(categoryId1);
            if (category != null) {
                dishDto.setCategoryName(category.getName());
            }

            LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DishFlavor::getDishId, item.getId());
            dishDto.setFlavors(dishFlavorService.list(wrapper));

            return dishDto;
        }).collect(Collectors.toList());
        return R.success(list1);
    }


}
